package com.newgrand.annexserver.storage;

import com.newgrand.annexcommon.constant.StoreStrateType;
import com.newgrand.annexcommon.model.FileEntity;
import com.newgrand.annexserver.beans.Dbconfig;
import com.newgrand.annexserver.storage.service.*;

/**
 * @author ：minyang
 * @date ：Created in 2019/9/8 21:50
 */
public class StoreStrategy {

    Dbconfig dbconfig;

    StorageService storageService;

    public StoreStrategy() {
        this.dbconfig = StorageContextHolder.getDbConfig();
        this.storageService = getCurrentStorageService(dbconfig);
    }

    private StorageService getCurrentStorageService(Dbconfig dbconfig) {
        int store_stratetype = dbconfig.getStoreStratetype();
        StorageService service = null;

        switch (store_stratetype) {
            case StoreStrateType.mysql:
                service = new MysqlStorage(dbconfig);
                break;
            case StoreStrateType.oracle:
                service = new OracleStorage(dbconfig);
                break;
            case StoreStrateType.sqlserver:
                service = new SqlserverStorage(dbconfig);
                break;
            case StoreStrateType.mongo:
                service = new MongoStorage(dbconfig);
                break;
            case StoreStrateType.fileSystem:
                service = new DefaultFileSystemStorage(dbconfig);
                break;
        }
        return service;
    }

    public String save(FileEntity entity) {
        return storageService.save(entity);
    }

    public String saveThumb(FileEntity entity) {
        return storageService.saveThumb(entity);
    }

    public FileEntity query(String id) {
        return storageService.query(id);
    }

    public void close() {
        storageService.close();
    }

    public FileEntity queryThumb(String id) {
        return storageService.queryThumb(id);
    }
}
